<?php

namespace Yolo\Components\Scheduler;

/**
 * Abstract class for the scheduler.
 * @author LUSHAOMING
 * @date  2024-12-13
 */
abstract class AbstractScheduler implements SchedulerInterface
{
    /**
     * Scheduler constructor.
     * @param array $config The scheduler config.
     */
    public function __construct(
        protected readonly array $config
    ) {
        $this->isRunRightAway = $this->config['runRightAway'] ?? false;
    }

    /**
     * @var bool $isRunRightAway The run right away status of the scheduler.
     */
    protected bool $isRunRightAway = false;

    /**
     * @var int $lastExecutionTimestamp The last execution timestamp of the scheduler.
     */
    protected int $lastExecutionTimestamp = 0;

    /**
     * @var int $nextExecutionTimestamp The next execution timestamp of the scheduler.
     */
    protected int $nextExecutionTimestamp = 0;

    /**
     * @var bool $isRunning The running status of the scheduler.
     */
    protected bool $isRunning = false;

    /**
     * Check if the scheduler should run right away.
     * @return bool
     */
    public function isRunRightAway(): bool
    {
        return $this->isRunRightAway;
    }

    /**
     * Set if the scheduler is running.
     * @param bool $running
     */
    public function setRunning(bool $running): void
    {
        $this->isRunning = $running;
    }

    /**
     * Check if the scheduler is running.
     * @return bool
     */
    public function isRunning(): bool
    {
        return $this->isRunning;
    }

    /**
     * Get the scheduler config.
     * @return array
     */
    public function getConfig(): array
    {
        return $this->config;
    }

    /**
     * Set the last execution timestamp.
     */
    public function setLastExecutionTimestamp(int $timestamp): void
    {
        $this->lastExecutionTimestamp = $timestamp;
    }

    /**
     * Set the next execution timestamp.
     */
    public function setNextExecutionTimestamp(int $timestamp): void
    {
        $this->nextExecutionTimestamp = $timestamp;
    }

    /**
     * Get the last execution timestamp.
     * @return int
     */
    public function getLastExecutionTimestamp(): int
    {
        return $this->lastExecutionTimestamp;
    }

    /**
     * Get the next execution timestamp.
     * @return int
     */
    public function getNextExecutionTimestamp(): int
    {
        return $this->nextExecutionTimestamp;
    }

    /**
     * Execute the scheduler.
     * @return void
     */
    abstract public function execute(): void;
}